package com.android.readcard.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

import com.android.readcard.dialog.ToSetBtDialog;
import com.android.readcard.inter.SetBtListener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 2017/8/24.
 */

public class BleGlobUtils {
    /**
     * 去除字符串中的空格、回车、换行符、制表符
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 字符串转bitmap
     *
     * @param string
     * @return
     */

    public Bitmap stringtoBitmap(String string) {
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存bitmap到本地
     *
     * @param bitmap
     */
    public static String saveIDIcon(final Bitmap bitmap) {
        File file = new File(GlobalPathUtils.getFileDirectory() + GlobalPathUtils.STR_PATH);
        file.mkdirs();
        String id_icon = GlobalPathUtils.getFileDirectory() + GlobalPathUtils.getDateFormatPhotoPath();
        Log.e("项目异常", "------id_icon------" + id_icon);
        FileOutputStream bos = null;
        try {
            bos = new FileOutputStream(id_icon);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            if (bos != null) {
                bos.flush();
            }
            if (bos != null) {
                bos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("项目异常", "------Exception------" + e.toString());
            return "异常";
        }
        return id_icon;
    }

    /**
     * bitmap转为base64
     */
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将Bitmap转换成字符串
     */
    public static String bitmaptoString(Bitmap bitmap) {
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

    public static Boolean isNum(String str) {
//        Pattern p = Pattern.compile("[0-9]+?");
//        Matcher m = p.matcher(str);
//        if (m.matches()) {
//            // 包含数字
//            return true;
//        } else {
//            // 不包含数字
//            return false;
//        }
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(str);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 配对弹窗
     */
    public static void setDialog(final Activity activity) {
        ToSetBtDialog toSetBtDialog = new ToSetBtDialog(activity, new SetBtListener() {

            @Override
            public void clickButn() {
                activity.finish();
            }
        });
        toSetBtDialog.show();
    }
}
